#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcustomplot.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ren = new Render(ui,&data);
    getRequest();
}

void MainWindow::getRequest()
{
    manager = new QNetworkAccessManager(this);

    request.setUrl(QUrl("https://c.m.163.com/ug/api/wuhan/app/data/list-total"));
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json;charset=utf-8"));
    reply = manager->get(request);
    connect(reply,&QNetworkReply::finished,this,&MainWindow::getData);
}

void MainWindow::getData()
{
    if (reply->error()){
        qDebug()<<reply->errorString();
        reply->deleteLater();
        return;
    }
    jsonData = reply->readAll();
    parseJson();
}


/**
 * @brief 解析json
 */
void MainWindow::parseJson()
{
    //qDebug() << jsonData;
    document = QJsonDocument::fromJson(jsonData.toUtf8(),&parseJsonErr);
    if(parseJsonErr.error != QJsonParseError::NoError){
        qDebug()<<"json error:" << parseJsonErr.errorString();
    }
    else
    {
        QJsonObject root_Obj = document.object();
        if (root_Obj.value("code").toInt() != 10000)
        {
            qDebug()<<"request error:" ;
            return;
        }
        data.data = root_Obj.value("data").toObject();
        data.chinaTotal = data.data.value("chinaTotal").toObject();
        data.chinaDayList = data.data.value("chinaDayList");
        data.today = data.chinaTotal.value("today").toObject();
        data.total = data.chinaTotal.value("total").toObject();
        data.extData = data.chinaTotal.value("extData").toObject();
        data.areaTree = data.data.value("areaTree");

        data.bannerData.todayInput = data.today.value("input").toInt();
        data.bannerData.incrNoSymptom= data.extData.value("incrNoSymptom").toInt();
        data.bannerData.todayStoreConfirm = data.today.value("storeConfirm").toInt();
        data.bannerData.todayConfirm = data.today.value("confirm").toInt();
        data.bannerData.todayDead = data.today.value("dead").toInt();
        data.bannerData.todayHeal = data.today.value("heal").toInt();

        data.bannerData.totalInput = data.total.value("input").toInt();
        data.bannerData.totalNoSymptom = data.extData.value("noSymptom").toInt();
        data.bannerData.totalConfirm = data.total.value("confirm").toInt();
        data.bannerData.totalDead = data.total.value("dead").toInt();
        data.bannerData.totalHeal = data.total.value("heal").toInt();
        data.bannerData.totalStoreConfirm = data.bannerData.totalConfirm-data.bannerData.totalDead-data.bannerData.totalHeal;

        if (data.chinaDayList.isArray())
        {
            data.chinaDayListArr = data.chinaDayList.toArray();
            ui->widget->setLocale(QLocale(QLocale::Chinese, QLocale::China));
            for (int i = data.chinaDayListArr.size()-1; i >= 0; i-=3 )
            {
                QJsonValue chinaDayListValue = data.chinaDayListArr.at(i);
                QJsonObject chinaDayListObject = chinaDayListValue.toObject();
                QString str = chinaDayListObject.value("date").toString();
                data.date.push_front(str);
                QJsonObject chinaDayListTotal = chinaDayListObject.value("total").toObject();
                QJsonObject chinaDayListToday = chinaDayListObject.value("today").toObject();
                double todayConfirm = chinaDayListToday.value("confirm").toDouble();
                double confirm = chinaDayListTotal.value("confirm").toDouble();
                double storeConfirm = chinaDayListTotal.value("storeConfirm").toDouble();
                double dead = chinaDayListTotal.value("dead").toDouble();
                double heal = chinaDayListTotal.value("heal").toDouble();
                data.totalDeadArr.push_front(dead);
                data.totalHealArr.push_front(heal);
                data.todayConfirmArr.push_front(todayConfirm);
                data.storeConfirmArr.push_front(storeConfirm);
                data.totalConfirmArr.push_front(confirm);
                data.dateArr.push_front(i);
            }

        }

        if (data.areaTree.isArray())
        {
            data.areaArr = data.areaTree.toArray();
            for (int i = 0; i < data.areaArr.size(); i++)
            {
                QJsonObject WorldAreaValue = data.areaArr.at(i).toObject();
                QJsonObject WorldAreaToday = WorldAreaValue.value("today").toObject();
                QJsonObject WorldAreaTotal = WorldAreaValue.value("total").toObject();

                DiseaseData tempData;
                tempData.id = WorldAreaValue.value("id").toString();
                tempData.todayConfirm = WorldAreaToday.value("confirm").toInt();
                tempData.totalConfirm = WorldAreaTotal.value("confirm").toInt();
                tempData.totalDead = WorldAreaTotal.value("dead").toInt();
                tempData.totalHeal = WorldAreaTotal.value("heal").toInt();
                tempData.areaName = WorldAreaValue.value("name").toString();
                data.worldData.push_back(tempData);


                if (tempData.id == "0")
                {
                    data.china = WorldAreaValue.value("children");
                    if (data.china.isArray())
                    {
                        data.chinaArr = data.china.toArray();
                        for(int i = 0; i < data.chinaArr.size(); i++)
                        {
                            QJsonObject chinaAreaValue = data.chinaArr.at(i).toObject();
                            QJsonObject chinaAreaToday = chinaAreaValue.value("today").toObject();
                            QJsonObject chinaAreaTotal = chinaAreaValue.value("total").toObject();

                            DiseaseData chinaTempData;
                            chinaTempData.areaName = chinaAreaValue.value("name").toString();
                            chinaTempData.totalConfirm = chinaAreaTotal.value("confirm").toInt();
                            chinaTempData.totalHeal = chinaAreaTotal.value("heal").toInt();
                            chinaTempData.totalDead = chinaAreaTotal.value("dead").toInt();
                            chinaTempData.todayConfirm = chinaAreaToday.value("confirm").toInt();
                            chinaTempData.totalStoreConfirm = chinaTempData.totalConfirm - chinaTempData.totalHeal - chinaTempData.totalDead;

                            QJsonValue chinaAreaChindren = chinaAreaValue.value("children");
                            if (chinaAreaChindren.isArray())
                            {
                                QJsonArray chinaAreaChindrenArr = chinaAreaChindren.toArray();
                                if (!chinaAreaChindrenArr.isEmpty())
                                {
                                    for (int i = 0; i < chinaAreaChindrenArr.size(); i++)
                                    {
                                        QJsonObject cityValue = chinaAreaChindrenArr.at(i).toObject();
                                        QJsonObject cityTotal = cityValue.value("total").toObject();
                                        QJsonObject cityToday = cityValue.value("today").toObject();
                                        DiseaseData cityTempData;
                                        cityTempData.areaName = cityValue.value("name").toString();
                                        cityTempData.totalConfirm = cityTotal.value("confirm").toInt();
                                        cityTempData.totalHeal = cityTotal.value("heal").toInt();
                                        cityTempData.totalDead = cityTotal.value("dead").toInt();
                                        cityTempData.todayConfirm = cityToday.value("confirm").toInt();
                                        cityTempData.totalStoreConfirm = cityTempData.totalConfirm - cityTempData.totalHeal - cityTempData.totalDead;
                                        chinaTempData.children.push_back(cityTempData);
                                    }
                                }
                            }
                            data.chinaData.push_back(chinaTempData);
                        }
                    }
                }
            }
        }
        ren->bannerRender(&data.bannerData);
        ren->WorldTreeRender(data.worldData);
        ren->ChinaTreeRender(data.chinaData);
        ren->plot(&data);
    }
}


MainWindow::~MainWindow()
{
    delete ui;
}


